<?php

namespace app\admin\model;

use think\Model;

class UserNotice extends Model
{
    protected $autoWriteTimestamp = true;

    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        if (input('get.title')) $where[] = ['a.title', 'like', "%" . input('get.title') . "%"];
        $list = self::alias('a')
            ->join('user_list b', 'a.user_id = b.id', 'left')
            ->order('a.id', 'desc')
            ->field('a.*, b.name')
            ->where($where)
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $list[$k]['name'] = $v['name'] ?? '全体用户';
            if ($v['user_id'] == 0) $list[$k]['read'] = NoticeRead::getNoticeReadCount($v['id']);
            else $list[$k]['read'] = (bool)NoticeRead::getNoticeRead($v['id'], $v['user_id']);
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }
}